import Notify from '@vant/weapp/notify/notify';
import axios from '../../utils/axios';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    planname:'',
    planmodes:["学习","娱乐","交友"],
    weekday:['周日','周一','周二','周三','周四','周五','周六'],
    currentindex:1,
    date:'请选择',
    remarks:'',
    remindmodes:['从不','每天','每周','每月'],
    remindmode:"从不",
    time:'请选择',
    top:false,
    cron:'',
    isremark:false
  },
  save:function(){
    if(this.data.date=="请选择"||this.data.time=="请选择"){
      Notify({
        message: '请正确选择时间',
        color: '#FFF',
        background: '#C5CEFD',
      });
      return;
    }else{
      var datetime=this.stringToDate(this.data.date+" "+this.data.time+":00")
      if(datetime.getTime()-new Date().getTime()<0){
        Notify({
          message: '请正确选择时间',
          color: '#FFF',
          background: '#C5CEFD',
        });
        return
      }else{
    var time=this.data.time
    var remindmode=this.data.remindmode
    var date=this.data.date
    var oplantime=new Date(new Date(date+"T"+time).getTime()-8*60*60*1000)
    console.log(oplantime)
    var cron=0+" "+(oplantime.getMinutes()+1)+" "+oplantime.getHours()
    switch (remindmode){
      case "从不": 
      cron=cron+" "+oplantime.getDate()+" "+(oplantime.getMonth()+1)+" ? "+oplantime.getFullYear()
      break
      case "每天": 
      cron=cron+" * * ? *"
      break;
      case "每周":
      cron=cron+" "+oplantime.getDate()+"/7"+" "+oplantime.getDate()+" ? "+oplantime.getFullYear()
      break;
      case "每月":
      cron=cron+" "+oplantime.getDate()+" * ? *"
    }
    
    console.log(cron)
      var that=this
    wx.showModal({
      title:"是否确认保存",
      success:function(res){
        if(res.confirm){
          var token=getApp().globalData.accessToken
          console.log(token)
          console.log(that.data.cron)
          var data={
            isAllDay:true,
            cron:cron,
            isMind:that.data.isremark,
            isTop:that.data.top,
            mode:that.data.planmodes[that.data.currentindex],
            name:that.data.planname,
            remarks:that.data.remarks,
          }
          axios.post('/plan',data)
          wx.showToast({
            title:"添加成功"
          })
          setTimeout(() => {
            wx.switchTab({
              url:"../calendar/calendar"
            })
          }, 500);
        }
      }
    })
    }

      }
      
    
  },
  stringToDate(dateStr){

        //若月份、日期、小时、分钟、秒未满10的情况，取整时祛除下前面的0(如04转为4)，否则取整会失败。
    
        let fullYear = parseInt(dateStr.substring(0,4));
    
        let month = parseInt(dateStr.substring(5, 7).substring(0, 1) == '0' ? dateStr.substring(6, 7) : dateStr.substring(5, 7));
    
        let day = parseInt(dateStr.substring(8, 10).substring(0, 1) == '0' ? dateStr.substring(9, 10) : dateStr.substring(8, 10));
    
        let hour = parseInt(dateStr.substring(11, 13).substring(0, 1) == '0' ? dateStr.substring(12, 13) : dateStr.substring(11, 13));
    
        let minute = parseInt(dateStr.substring(14, 16).substring(0, 1) == '0' ? dateStr.substring(15, 16) : dateStr.substring(14, 16));
    
        let second = parseInt(dateStr.substring(17, 19).substring(0, 1) == '0' ? dateStr.substring(18, 19) : dateStr.substring(17, 19));
    
        return new Date(fullYear,month-1,day,hour,minute,second);
    
      },
  getplandetail:function(e){
    if(e.type=='input'){
      if(e.currentTarget.id=='planname'){
        this.setData({
          planname:e.detail.value
        })
      }else{
        this.setData({
          remarks:e.detail.value
        })
      }
    }else{
      if(e.type=='tap'){
        this.setData({
          currentindex:e.currentTarget.dataset.index
        })
      }else{
        if(e.currentTarget.id=="date"){
        
          this.setData({
            date:e.detail.value
          })
        }else{
          if(e.currentTarget.id=="time"){
            this.setData({
              time:e.detail.value
            })
          }else{
            if(e.currentTarget.id=="remindmode"){
              this.setData({
                remindmode:this.data.remindmodes[e.detail.value]
              })
            }else{
              if(e.currentTarget.id=="isremind"){
                this.setData({
                  isremark:!this.data.isremark
                })
              }else{
                this.setData({
                  top:!this.data.top
                })
              }
            }
          }
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})